package com.tj.collection.controller.admin.request;


import com.fqgj.common.api.ParamsObject;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApiIllegalArgumentException;
import com.tj.collection.common.RequestLocalInfo;
import com.tj.collection.db.domain.Admin;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2016/11/22
 * Time: 上午11:55
 */
public class AddAdminVo extends ParamsObject {
    /**
     * 账号
     */
    private String name;
    private String password;
    /**
     * 名字
     */
    private String fullName = "";
    private Integer type;
    private String account;
    private String telephone;
    private String productCode;
    private Integer productAuthId;
    private List<Long> roles = new ArrayList<>();

    public String getAccount() {
        return account;
    }

    public AddAdminVo setAccount(String account) {
        this.account = account;
        return this;
    }

    public String getTelephone() {
        return telephone;
    }

    public AddAdminVo setTelephone(String telephone) {
        this.telephone = telephone;
        return this;
    }

    public List<Long> getRoles() {
        return roles;
    }

    public AddAdminVo setRoles(List<Long> roles) {
        this.roles = roles;
        return this;
    }

    public String getName() {
        return name;
    }

    public AddAdminVo setName(String name) {
        this.name = name;
        return this;
    }

    public String getPassword() {
        return password;
    }

    public AddAdminVo setPassword(String password) {
        this.password = password;
        return this;
    }

    public String getFullName() {
        return fullName;
    }

    public AddAdminVo setFullName(String fullName) {
        this.fullName = fullName;
        return this;
    }

    public Integer getType() {
        return type;
    }

    public AddAdminVo setType(Integer type) {
        this.type = type;
        return this;
    }

    public String getProductCode() {
        return productCode;
    }

    public AddAdminVo setProductCode(String productCode) {
        this.productCode = productCode;
        return this;
    }

    public int getProductAuthId() {
        return productAuthId;
    }

    public AddAdminVo setProductAuthId(int productAuthId) {
        this.productAuthId = productAuthId;
        return this;
    }

    @Override
    public void validate() {
        if (StringUtils.isEmpty(name)) {
            throw new ApiIllegalArgumentException("Account can not be empty");
        }
        if (StringUtils.isEmpty(fullName)) {
            fullName = name;
        }
        if (StringUtils.isEmpty(account)) {
            throw new ApiIllegalArgumentException("Phone number can not be empty");
        }
        /*if (!PasswordUtil.isPasswordValidate(password)) {
            throw new ApiIllegalArgumentException("密码格式错误");
        }*/
        if (StringUtils.isEmpty(productCode)) {
            throw new ApiIllegalArgumentException("Product code can not be empty");
        }
        if (productCode.equals("sdzzall") || productCode.equals("sdzzold")) {
            if (null == productAuthId || productAuthId < 0) {
                throw new ApiIllegalArgumentException("闪电周转新增复借权限不能为空");
            }
        }
        if (CollectionUtils.isEmpty(roles)) {
            throw new ApiIllegalArgumentException("Role can not be empty");
        }
    }

    public Admin trans2Admin() {
        if (StringUtils.isEmpty(fullName)) {
            fullName = name;
        }

        return new Admin().setName(fullName)
                .setAccount(account)
                .setPhone(telephone)
                .setProductCode(productCode)
                .setRoles(roles)
                .setProductAuthId(productAuthId)
                .setMerchantCode(RequestLocalInfo.getCurrentAdmin().getMerchantCode())
                .setPassword(StringUtils.isEmpty(password) ? "" : password);
    }


}
